//! 数据网关集成测试
//!
//! 验证微内核+插件架构的完整数据查询流程

use data_gateway_integration_tests::{create_test_kernel, cleanup_test_kernel};
use data_gateway_parser::{parse_script, execute_script, ScriptContext};
use data_gateway_data_layer::ArrowBridge;
use serde_json::json;

/// 测试微内核系统的基本功能
#[tokio::test]
async fn test_kernel_basic_functionality() {
    let kernel = create_test_kernel().await;

    // 验证内核创建成功
    assert!(kernel.get_status().await.is_ok());

    // 清理
    cleanup_test_kernel(kernel).await;
}

/// 测试脚本解析和执行
#[tokio::test]
async fn test_script_parsing_and_execution() {
    // 测试脚本解析
    let script = r#"
        var x = 10;
        var y = 20;
        var result = x + y;
        print("Result: " + result);
    "#;
    
    let program = parse_script(script).unwrap();
    assert_eq!(program.statements.len(), 4);
    
    // 测试脚本执行
    let context = ScriptContext::new();
    let execution_result = execute_script(script, context).await.unwrap();
    
    assert!(execution_result.stats.statements_executed > 0);
    assert_eq!(execution_result.stats.function_calls, 1); // print函数调用
}

/// 测试Arrow数据桥接器
#[tokio::test]
async fn test_arrow_bridge_functionality() {
    let mut bridge = ArrowBridge::new();

    // 测试JSON到Arrow转换
    let json_data = vec![
        json!({"id": 1, "name": "Alice", "score": 95.5}),
        json!({"id": 2, "name": "Bob", "score": 87.2}),
    ];

    let record_batch = bridge.json_to_arrow(&json_data).unwrap();

    assert_eq!(record_batch.num_rows(), 2);
    assert_eq!(record_batch.num_columns(), 3);

    // 验证Schema
    let schema = record_batch.schema();
    assert_eq!(schema.fields().len(), 3);
}


